<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item
        :label="`${$courseName ? $courseName : '课程'}名称`"
        prop="courseName"
      >
        <el-input
          v-input-restrict
          v-model="queryParams.courseName"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
          filterable
          v-model="queryParams.type"
          placeholder="请选择"
          clearable
        >
          <el-option label="必修" :value="0" />
          <el-option label="选修" :value="1" />
          <el-option label="考证" :value="2" />
          <el-option label="工学" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item
        :label="`${$gradeName ? $gradeName : '班级'}`"
        prop="classId"
      >
        <el-select
          filterable
          v-model="queryParams.classId"
          placeholder="请输入"
          clearable
        >
          <el-option
            v-for="(item, index) in classes"
            :key="index"
            :label="item.className"
            :value="item.classId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学生" prop="studentName">
        <el-input
          v-input-restrict
          v-model="queryParams.studentName"
          placeholder="请输入学生"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          v-preventReClick
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button
          v-preventReClick
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="postList">
      <el-table-column
        :label="`${$courseName ? $courseName : '课程'}编号`"
        align="center"
        prop="postId"
      />
      <el-table-column
        :label="`${$courseName ? $courseName : '课程'}`"
        align="center"
        prop="postCode"
      />
      <el-table-column
        :label="`${$workstationName ? $workstationName : '工作站'}`"
        align="center"
        prop="postName"
      />
      <el-table-column label="主讲老师" align="center" prop="postSort" />
      <el-table-column label="成绩" align="center" prop="postSort" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-preventReClick
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="title"
      :visible.sync="open"
      width="800px"
      append-to-body
    >
      <el-form @submit.native.prevent ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item
              :label="`${$gradeName ? $gradeName : '班级'}名称`"
              prop="postName"
            >
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              :label="`${$branchName ? $branchName : '分院'}名称`"
              prop="postName"
            >
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              :label="`${$majorName ? $majorName : '专业'}名称`"
              prop="postName"
            >
            </el-form-item>
          </el-col>
        </el-row>
        <div class="titleLineGreen" style="margin: 0 0 15px 0">
          已选{{ $courseName ? $courseName : "课程" }}
        </div>
        <el-table v-loading="loading" :data="postList">
          <el-table-column
            :label="`${$courseName ? $courseName : '课程'}`"
            align="center"
            prop="postId"
          />
          <el-table-column label="编号" align="center" prop="postCode" />
          <el-table-column label="主讲老师" align="center" prop="postName" />

          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                v-preventReClick
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleDiaListDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="titleLineGreen" style="margin: 25px 0 15px 0">
          添加{{ $courseName ? $courseName : "课程" }}
        </div>
        <el-select
          filterable
          v-model="form.courseId"
          :placeholder="`添加${$courseName ? $courseName : '课程'}`"
          clearable
          filterable
        >
          <el-option
            v-for="(item, index) in courses"
            :key="index"
            :label="item.courseName"
            :value="item.courseId"
          />
        </el-select>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-preventReClick @click="cancel">取 消</el-button>
        <el-button v-preventReClick type="primary" @click="submitForm"
          >确 定</el-button
        >

      </div>
    </el-dialog>
  </div>
</template>

<script>
//TODO 只写了全局模板
import {
  listPost,
  getPost,
  delPost,
  addPost,
  updatePost,
} from "@/api/system/post";
import { listPost_class } from "@/api/schoolOrganization/organization";
import { listPost_course } from "@/api/course/course";
export default {
  name: "Post",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseName: undefined,
        studentName: undefined,
        classId: undefined,
        type: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

      classes: [],
    };
  },
  created() {
    this.getList();
    this.getClass();
  },
  methods: {
    getClass() {
      listPost_class({
        pageNum: 1,
        pageSize: 9999,
        schoolId: this.$store.getters.schoolId,
      }).then((res) => {
        this.classes = res.rows;
      });
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      this.queryParams.schoolId = this.$store.getters.schoolId;
      listPost(this.queryParams).then((response) => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        postId: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: 0,
        status: "0",
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      //   this.reset();
      //   const postId = row.postId || this.ids;
      //   getPost(postId).then((response) => {
      //     this.form = response.data;
      //     this.open = true;
      //     this.title = "编辑";
      //   });
      this.$router.push({
        path: "/course/scoreStatisticsDetail",
        query: {
          id: row.postId,
        },
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.postId != undefined) {
            updatePost(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPost(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDiaListDelete(row) {
      const postIds = row.postId || this.ids;
      this.$modal
        .confirm("确定删除")
        .then(function () {
          return delPost(postIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
